package com.cqs.leetcode.math;

/**
 * @author lixiaowen
 * @create 2019-12-16
 */
public class DivideTwointegers29 {

    public int divide(int dividend2, int divisor2) {
        if (dividend2 == 0) return 0;
        if (divisor2 == 1) return dividend2;
        if (divisor2 == -1){
            if(dividend2 == 0x80000000){
                return 0x7FFFFFFF;
            }
            return  0 - dividend2;
        }         int  sign = (dividend2>0 && divisor2 > 0) || (dividend2 < 0 && divisor2 < 0 ) ? 1 : 0;
        int count = 0;
        long dividend = Math.abs(Long.valueOf(dividend2));
        long divisor = Math.abs(Long.valueOf(divisor2));
        while (dividend >= divisor) {
            dividend -= divisor;
            count++;
        }
        return  sign > 0 ? count : (0 - count);
    }

    public static void main(String[] args) {
        DivideTwointegers29 divide = new DivideTwointegers29();
        int[][] cases= {{100,20},{-3,-1},{0,100},{37,3},{Integer.MIN_VALUE,-1},{Integer.MIN_VALUE,1},{Integer.MIN_VALUE,Integer.MIN_VALUE}};
        for (int i = 4; i < cases.length ; i++) {
            int result = divide.divide(cases[i][0],cases[i][1]);
            System.out.println(cases[i][0]+ " / " +cases[i][1] + " = " + result);
        }
        System.out.println(0xFFFFFFFF & 0x123efd );
        System.out.println(0x123efd );
        System.out.println(0-Integer.MIN_VALUE);
        System.out.println(0xFFFFFFFF & 0xFFFFFFFF);
    }
}
